package demo2;


import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @description: DBCP连接池的测试
 * @author: apple
 * @date:  2022/1/29 6:57 下午
 * @version 1.0
 **/
public class DBCPTest {

    // 方式一
    @Test
    public void testConnection() throws SQLException {

        // control + h 查找实现类

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        dataSource.setInitialSize(10);
        dataSource.setMaxActive(10);

        final Connection connection = dataSource.getConnection();

        System.out.println(connection);

    }

    @Test
    public void testConnection2() throws Exception {

        Properties properties = new Properties();
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
        properties.load(resourceAsStream);
        DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();

        System.out.println(connection);

    }


}
